package com.liuwei.learning.service;

import com.liuwei.learning.dao.ArticleJDBCDAO;
import com.liuwei.learning.jpa.testdb1.Article;
import com.liuwei.learning.jpa.testdb1.ArticleRepository;
import com.liuwei.learning.model.ArticleVO;
import com.liuwei.learning.utils.DozerUtils;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ArticleRestJDBCServiceImpl implements ArticleRestService {

    @Resource
    private ArticleJDBCDAO articleJDBCDAO;
    @Resource
    private JdbcTemplate primaryJdbcTemplate;

    @Resource
    private ArticleRepository articleRepository;

    @Resource
    private JdbcTemplate secondaryJdbcTemplate;


    @Override
    public ArticleVO saveArticle(ArticleVO article) {
        return null;
    }

    @Override
    public void deleteArticle(Long id) {
        articleJDBCDAO.deleteById(id,primaryJdbcTemplate);
    }

    @Override
    public void updateArticle(ArticleVO article) {

    }

    @Override
    public ArticleVO getArticle(Long id) {
        return null;
    }

    @Override
    public List<ArticleVO> getAll() {

        Pageable pageable = PageRequest.of(0,5);
        Page<Article> articlePage = articleRepository.findAll(pageable);
        List<Article> articles = articlePage.getContent();
        return DozerUtils.mapList(articles,ArticleVO.class);
    }
}
